package com.itheima.health.mobile.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.mobile.vo.SmsLoginParam;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("mobile/login")
@Slf4j
public class LoginController {
    @DubboReference
    private MemberService memberService;
    @DubboReference
    private SmsService smsService;

    /**
     * @Author: 位帅帅
     * @Date: 10:06 2021/1/11
     * @Params: [param]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 用户登录
    */
    @PostMapping("smsLogin")
    public Result SmsLogin(@RequestBody SmsLoginParam param) {
        log.info("[用户登录-参数]param: {}",param);
        //验证码服务
        boolean flag = smsService.checkValidateCode("MOBILE_SIGNIN", param.getTelephone(), param.getValidateCode());
        if (!flag) {
            return new Result(false,MessageConst.VALIDATECODE_ERROR);
        }
        //添加或更新用户
        Member member = new Member();
        member.setPhoneNumber(param.getTelephone());
        member = memberService.addOrUpdate(member);
        log.info("[用户登录-结果]member: {}",member);
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }
}
